package org.fintan.make.service;

import java.util.Date;
import java.util.List;

import org.fintan.make.dao.UserMapper;
import org.fintan.make.entity.User;
import org.fintan.make.master.ExecuteResult;
import org.fintan.make.params.UserParam;
import org.fintan.make.utils.MD5util;
import org.fintan.make.utils.UUIDutil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserService {
	private Logger log = LoggerFactory.getLogger(getClass());
	@Autowired
	private UserMapper userMapper;
	/*
	 * 注册用户信息
	 */
	@Transactional
	public boolean insert(User user){
		User ser = userMapper.selectUserByUserName(user.getUserName());
		if(ser != null){
			log.info("用户名已经存在："+user.getUserName());
			return false;
		}
		user.setSalt(UUIDutil.getUUID());	
		String password = user.getSalt()+user.getPassword();
		user.setPassword(MD5util.MD5(password));
		user.setAddDate(new Date());
		userMapper.insertSelective(user);
		return true;
	}
	
	/*
	 * 根据userId来查询用户信息
	 */
	@Transactional
	public ExecuteResult<?> updateUser(User user){
		log.info("要修改的用户id为："+ user.getId());
		if(user.getId() == null){
			log.info("修改的用户id为空");
			return ExecuteResult.fail("修改的用户id为空");
		}
		if(userMapper.updateByPrimaryKeySelective(user) > 0){
			log.info("修改成功");
			return ExecuteResult.ok("修改成功");
		}
		return ExecuteResult.fail("");
	}
	/*
	 * 根据用户id进行删除信息
	 */
	@Transactional
	public ExecuteResult<?> delectUser(Integer id){
		if(id == null){
			log.info("用户id为空，删除失败");
			return ExecuteResult.fail("用户id为空");
		}
		log.info("要删除的用户id为："+id);
		if(userMapper.deleteByPrimaryKey(null)>0){
			log.info("删除成功");
			return ExecuteResult.ok("删除成功");
		}
		return ExecuteResult.fail("删除失败");
	}
	

	/*
	 * 查询全部用户信息
	 */
	public List<User> selectUserByCondition(UserParam param){
		return userMapper.selectUserByCondition(param);
	}
	/*
	 * 根据id查询用户
	 */
	public User selectUserById(Integer id){
		
		return userMapper.selectByPrimaryKey(id);
	}
	
	
}
